//用户管理

var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami');


/* GET users listing. */
router.get('/',async function (req, res, next) {
  // //获取用户名
  let username = req.session.username;
  //通过获取用户名查找所有的信息
  let sqlStr = 'select * from user where username = ?';
  let result = await sqlQuery(sqlStr,[username]);
  let user = result[0];
  //通过角色表获取所有角色
  let role = await getRole()

  let options = {user,role};
  res.render('admin/user/info',options);
});


//获取用户角色
async function getRole(){
  let sqlStr = 'select * from role'
  let result = await sqlQuery(sqlStr);
  //结果返回
  return Array.from(result);
}


//获取修改的用户信息
router.post('/',async(req,res)=>{
  console.log(req.body);
  let password = jiami(req.body.password)
  let email = req.body.email
  let phone = req.body.phone
  let roleid = req.body.roleid
  let username = req.body.username

  //更新数据
  let sqlStr = "update user set password=?, email=?, phone=?, roleid=? where username=? ";
  let arr = [password,email,phone,roleid,username]
  await sqlQuery(sqlStr,arr);
      res.json({
        state:"ok",
        content:"个人信息更新成功"
      })
})


module.exports = router;
